package com.liuqing.www.java基础知识.网络通信;


import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
 * @author kevinbian E-mail: langzibian@yahoo.com.cn
 * @version 创建时间：2011-5-13 下午01:39:01
 * 
 */
public class NewThread {
	public static void main(String[] args) {
		ExecutorService threadpool=Executors.newCachedThreadPool()  ;//始终只有2个线程在运行
		for(int i=0;i<1000;i++){
			
			Thread t=new Thread(new SocketClient());
			threadpool.execute(t);
		}
		new Thread(new SocketClient()).start();
//		new Thread(new SocketClient()).start();
	}
	public NewThread() {

	}
}
class HttpClient implements Runnable {
	public void run() {
		String content = "" ;
		URL url;
		try {
//			Socket socket = new Socket("192.168.1.200" , 8080) ;
//			socket.getInputStream() ;
			System.out.println("1111");
			url = new URL("http://192.168.1.200:8080/chuhan/online.jsp");
			while(true){
				URLConnection conn = url.openConnection();
				BufferedReader is = new BufferedReader(new InputStreamReader(conn.getInputStream())) ;
				while(is.readLine() != null){
					content += is.readLine() ;
					System.out.println(content);
				}
				Thread.sleep(2000) ;
			}
		
//			is.close() ;
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}                                           //说明是get请求和http的版本
class SocketClient implements Runnable{
	public void run() {
		try {
//			http://www.fkjava.com/
//			http://zhidao.baidu.com/q?word=%C8%F8%B7%A8&ct=17&pn=0&tn=ikaslist&rn=10&lm=0&fr=search
			StringBuffer sb = new StringBuffer("GET /article/3/1356877008358819398.html HTTP/1.1\r\n"); 
			sb.append("User-Agent: Java/1.6.0_20\r\n"); 
			sb.append("Host:www.fkjava.com \r\n"); 
			sb.append("Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n"); 
			sb.append("keep-alive 300\r\n"); 
			sb.append("Connection: keep-alive\r\n"); 
			sb.append("\r\n");
//			OutputStream out = socket.getOutputStream(); 
//			BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(),"utf8")); 
//			OutputStream out=socket.getOutputStream();
//			InputStream in=socket.getInputStream();
			while(true){
				try {
					Socket socket = new Socket("www.fkjava.com" , 80) ;
					OutputStream out = socket.getOutputStream();
					out.write(sb.toString().getBytes("utf8")); 
//					String tmp = ""; 
//					while((tmp = br.readLine())!=null){ 
//						System.out.println(tmp); 
//					}
					System.out.println("完毕！！！");
					out.flush();
//				Thread.sleep(1000);
				}catch(Exception e){
					continue;
				}
			}
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
